<?php

class BolaoEventPeer extends BaseBolaoEventPeer
{
  public static function getMonthEvents($month, $year)
  {
    $criteria = BolaoEventPeer::getCriteriaForEventsAt($year, $month);
    return BolaoEventPeer::doSelect($criteria);
  }
  
  public static function organizeEventsByDay($events)
  {
    $events_by_day = array();
    foreach($events as $event)
    {
      $current_day = strtotime($event->getStartDate());
      if ($event->getEndDate() != null)
        $last_day = strtotime($event->getEndDate());
      else
        $last_day = $current_day;

      while ($current_day <= $last_day)
      {
        $day_index = date('Y-m-d', $current_day);
        if (!array_key_exists($day_index, $events_by_day))
          $events_by_day[$day_index] = array();

        $events_by_day[$day_index][] = $event;

        $current_day += 24 * 60 * 60;  //increment one day
      }
    }

    return $events_by_day;
  }

  public static function getNextEvents($number_of_events = -1)
  {
    $start_date = date('Y-m-d');

    $criteria = new Criteria();
    $cron1 = $criteria->getNewCriterion(BolaoEventPeer::START_DATE, $start_date, Criteria::GREATER_EQUAL);
    $cron2 = $criteria->getNewCriterion(BolaoEventPeer::END_DATE, $start_date, Criteria::GREATER_EQUAL);
    $cron1->addOr($cron2);
    $criteria->add($cron1);
    if ($number_of_events > 0) $criteria->setLimit($number_of_events);
    $criteria->addAscendingOrderByColumn(BolaoEventPeer::START_DATE);

    return BolaoEventPeer::doSelect($criteria);
  }

  public static function getCriteriaForEventsAt($year, $month = -1, $day = -1)
  {
    if ($day > 0 && $month > 0)
    {
      // Mount criteria for a specific day
      $date = sprintf("%04d-%02d-%02d", $year, $month, $day);

      $criteria = new Criteria();
      $cron1 = $criteria->getNewCriterion(BolaoEventPeer::START_DATE, $date, Criteria::LESS_EQUAL);
      $cron2 = $criteria->getNewCriterion(BolaoEventPeer::END_DATE, $date, Criteria::GREATER_EQUAL);
      $cron1->addAnd($cron2);

      $criteria->add(BolaoEventPeer::START_DATE, $date, Criteria::EQUAL);
      $criteria->addOr($cron1);
    }
    else
    {
      // Mount generic criteria for period of time
      if ($month == -1)
      {
        $begin_month = 1;
        $end_month = 12;
      }
      else
        $begin_month = $end_month = $month;

      $begin_date = sprintf('%04d-%02d-%02d', $year, $begin_month, 1);
      $end_date = date('Y-m-t', strtotime(sprintf('%04d-%02d-%02d', $year, $end_month, 1)));

      $criteria = new Criteria();
      // (start_date IN this period)
      $ctron1 = $criteria->getNewCriterion(BolaoEventPeer::START_DATE, $begin_date, Criteria::GREATER_EQUAL);
      $ctron2 = $criteria->getNewCriterion(BolaoEventPeer::START_DATE, $end_date, Criteria::LESS_EQUAL);
      $ctron1->addAnd($ctron2);
      // (end_date IN this period)
      $ctron3 = $criteria->getNewCriterion(BolaoEventPeer::END_DATE, $begin_date, Criteria::GREATER_EQUAL);
      $ctron4 = $criteria->getNewCriterion(BolaoEventPeer::END_DATE, $end_date, Criteria::LESS_EQUAL);
      $ctron3->addAnd($ctron4);
      // (start_date BEFORE this period AND end_date AFTER this period)
      $ctron5 = $criteria->getNewCriterion(BolaoEventPeer::START_DATE, $begin_date, Criteria::LESS_THAN);
      $ctron6 = $criteria->getNewCriterion(BolaoEventPeer::END_DATE, $end_date, Criteria::GREATER_THAN);
      $ctron5->addAnd($ctron6);

      // (start_date IN this month) OR 
      // (end_date IN this month) OR
      // (start_date BEFORE this period AND end_date AFTER this period)
      $ctron1->addOr($ctron3);
      $ctron1->addOr($ctron5);
      $criteria->add($ctron1);
    }

    // Add ascending order to found events
    $criteria->addAscendingOrderByColumn(BolaoEventPeer::START_DATE);

    return $criteria;
  }
}
